package com.credithc.customers.dataservice.service.impl;

import com.credithc.customers.base.constants.CustomerConstants;
import com.credithc.customers.dataservice.Thread.OldDataThreadPoolExecutor;
import com.credithc.customers.dataservice.constants.SyncTypeEnum;
import com.credithc.customers.dataservice.mapper.HHRCJD.HHRCFAccountMapper;
import com.credithc.customers.dataservice.mapper.HHRCJD.HHRCFOrderMapper;
import com.credithc.customers.dataservice.mapper.HHRCJD.HHRCFProductMapper;
import com.credithc.customers.dataservice.po.HHRCFAccountPO;
import com.credithc.customers.dataservice.po.HHRCFOrderPO;
import com.credithc.customers.dataservice.po.HHRCFProductPO;
import com.credithc.customers.dataservice.ro.CustomerAccountInfoRo;
import com.credithc.customers.dataservice.ro.CustomerProductInfoRo;
import com.credithc.customers.dataservice.ro.ProductInfoRo;
import com.credithc.customers.dataservice.service.OldHHRCFDataService;
import com.credithc.customers.dataservice.service.SyncDataService;
import com.credithc.customers.dataservice.until.OldDateUntil;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicReference;

import static com.credithc.customers.dataservice.service.impl.CustomerProductInfoServiceImpl.getYZTStatus;

@Service
public class OldHHRCFDataServiceImpl implements OldHHRCFDataService {
    @Autowired
    private SyncDataService syncDataService;
    @Autowired
    private OldDataThreadPoolExecutor oldDataThreadPoolExecutor;
    @Autowired
    private HHRCFAccountMapper hhrcfAccountMapper;
    @Autowired
    private HHRCFProductMapper hhrcfProductMapper;
    @Autowired
    private HHRCFOrderMapper hhrcfOrderMapper;


    @Override
    public void product(Integer start) {
        boolean f =true;
        Long num = null;
        if (start != null && start>0) {
            num = Long.valueOf(start);
        }else{
            num = hhrcfProductMapper.queryOrderMaxId();
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    Example example = new Example(HHRCFProductPO.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andBetween("p_id",vernier.get()-10000,vernier.get());
                    List<HHRCFProductPO> hhrcfProductPOS = hhrcfProductMapper.selectByExample(example);
                    if (hhrcfProductPOS.size() > 0) {
                        bulidhhrcfProduct(hhrcfProductPOS);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidhhrcfProduct(List<HHRCFProductPO> hhrcfProductPOS) {
        for (HHRCFProductPO h:hhrcfProductPOS) {
            ProductInfoRo productInfoRo = new ProductInfoRo();
            productInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            productInfoRo.setDatabaseName("finance");
            productInfoRo.setTableName("f_product");
            productInfoRo.setProductChannel("HHR_CF");
            productInfoRo.setSubSystemSign("HHR_CF");
            productInfoRo.setSystemSign("HHR");
            productInfoRo.setProductType(1+"");
            productInfoRo.setProductNo(h.getP_id());
            productInfoRo.setTableKey(h.getP_id());
            productInfoRo.setProductName(h.getP_name());
            productInfoRo.setProductCreateTime(h.getP_add_time());
            syncDataService.syncProduct(productInfoRo);
        }
    }

    @Override
    public void order(Integer start) {
        boolean f =true;
        Long num = null;
        if (start != null && start>0) {
            num = Long.valueOf(start);
        }else{
            num = hhrcfOrderMapper.queryOrderMaxId();
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    Example example = new Example(HHRCFOrderPO.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andBetween("id",vernier.get()-10000,vernier.get());
                    List<HHRCFOrderPO> hhrcfOrderPOS = hhrcfOrderMapper.selectByExample(example);
                    if (hhrcfOrderPOS.size() > 0) {
                        bulidhhrcfOrder(hhrcfOrderPOS);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidhhrcfOrder(List<HHRCFOrderPO> hhrcfOrderPOS) {
        for (HHRCFOrderPO h:hhrcfOrderPOS) {
            CustomerProductInfoRo customerProductInfoRo = new CustomerProductInfoRo();
            customerProductInfoRo.setDatabaseName("finance");
            customerProductInfoRo.setTableName("f_lender_detail");
            customerProductInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerProductInfoRo.setSubSystemSign("HHR_CF");
            customerProductInfoRo.setSystemSign("HHR");
            customerProductInfoRo.setProductSourceSystem(1+"");
            customerProductInfoRo.setProductAmount(h.getInvest_amt());
            customerProductInfoRo.setProductNo(h.getP_id());
            Example example = new Example(HHRCFProductPO.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("p_id",h.getP_id());
            HHRCFProductPO hhrcfProductPO = null;
            try {
                hhrcfProductPO = hhrcfProductMapper.selectOneByExample(example);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (hhrcfProductPO != null ) {
                customerProductInfoRo.setProductDeadline(hhrcfProductPO.getP_periods() * 30 + "");
            }
            customerProductInfoRo.setProductStartTime(h.getL_record_interest_time());
            customerProductInfoRo.setProductEndTime(h.getExpire_time());
            customerProductInfoRo.setTableKey(h.getId());
            customerProductInfoRo.setProductStatus(getYZTStatus("HHR_CF"+h.getL_status()));
            customerProductInfoRo.setUserResourceId(h.getU_id());
            Example example1 = new Example(HHRCFAccountPO.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("u_id",h.getU_id());
            HHRCFAccountPO hhrcfAccountPO = null;
            try {
                hhrcfAccountPO = hhrcfAccountMapper.selectOneByExample(example1); }
            catch (Exception e) {
                e.printStackTrace();
            }
            if (hhrcfAccountPO != null) {
                customerProductInfoRo.setCustomerNo(hhrcfAccountPO.getCustomerNo());
            }
            if (StringUtils.isNotBlank(customerProductInfoRo.getCustomerNo())) {
                syncDataService.syncCustomerProduct(customerProductInfoRo);
            }
        }
    }

    @Override
    public void account(Integer start) {
        boolean f =true;
        Long num = null;
        if (start != null && start>0) {
            num = Long.valueOf(start);
        }else{
            num = hhrcfAccountMapper.queryOrderMaxId();
        }
        if (num == null){
            return;
        }
        AtomicReference<Long> vernier = new AtomicReference<>( num+10001);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.execute(() -> {
                try {
                    if (vernier.get()>0){
                        vernier.set(vernier.get()-10000);
                    }
                    Example example = new Example(HHRCFOrderPO.class);
                    Example.Criteria criteria = example.createCriteria();
                    criteria.andBetween("u_id",vernier.get()-10000,vernier.get());
                    List<HHRCFAccountPO> hhrcfAccountPOS = hhrcfAccountMapper.selectByExample(example);
                    if (hhrcfAccountPOS.size() > 0) {
                        bulidhhrcfAccount(hhrcfAccountPOS);
                    }
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            });
            f = OldHyrDataServiceImpl.exit(f, vernier, e);
        }
    }

    private void bulidhhrcfAccount(List<HHRCFAccountPO> hhrcfAccountPOS) {
        for (HHRCFAccountPO h:hhrcfAccountPOS) {
            CustomerAccountInfoRo customerAccountInfoRo = new CustomerAccountInfoRo();
            customerAccountInfoRo.setSyncType(SyncTypeEnum.NEW.getValue());
            customerAccountInfoRo.setSubSystemSign("HHR_CF");
            customerAccountInfoRo.setSystemSign("HHR");
            customerAccountInfoRo.setAccountNo(h.getAccount_no());
            customerAccountInfoRo.setAccountCreateTime(h.getU_addtime());
            customerAccountInfoRo.setAccountStatus(CustomerConstants.STATUS_ACCOUNT_AVAILABLE);
            if (1 == h.getUser_role()){
                customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LEND);
                customerAccountInfoRo.setCustomerRole(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LEND);
            }else{
                customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
                customerAccountInfoRo.setCustomerRole(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_LOAN);
            }
            customerAccountInfoRo.setUserSourceId(h.getU_id());
            customerAccountInfoRo.setCustomerNo(h.getCustomerNo());
            if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())&& StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo())) {
                syncDataService.syncCustomerAccount(customerAccountInfoRo);
            }
            customerAccountInfoRo.setAccountType(CustomerConstants.CUSTOMER_ACCOUNT_TYPE_BANK_CARD);
            customerAccountInfoRo.setAccountNo(h.getA_card_num());
            customerAccountInfoRo.setCustomerRole(null);
            if (StringUtils.isNotBlank(customerAccountInfoRo.getAccountNo())&& StringUtils.isNotBlank(customerAccountInfoRo.getCustomerNo())) {
                syncDataService.syncCustomerAccount(customerAccountInfoRo);
            }
        }
    }
    @Value("${old.data.oldInOne}")
    private Integer oldInOne;
    @Override
    public void oldData() {
        if (oldInOne == null || oldInOne < 1) {
            oldInOne = 1000;
        }
        boolean f = true;
        Example example = new Example(HHRCFAccountPO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andCondition(" customerNo <> '' ");
        int num = 0;
        try {
            num = hhrcfAccountMapper.selectCountByExample(example);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (num < 1) {
            return;
        }
        Integer pageNum = num / oldInOne + 2;
        AtomicReference<Integer> vernier = new AtomicReference<>(pageNum);
        ExecutorService e = oldDataThreadPoolExecutor.getCustomThreadPoolExecutor();
        while (f) {
            e.submit(() -> {
                try {
                    if (vernier.get() >= 0) {
                        vernier.set(vernier.get() - 1);
                    }
                    RowBounds rowBounds = new RowBounds(vernier.get(), oldInOne);
                    List<HHRCFAccountPO> hhrcfAccountPOS = hhrcfAccountMapper.selectByExampleAndRowBounds(example, rowBounds);
                    if (hhrcfAccountPOS.size() > 0) {
                        bulidhhrcfAccount(hhrcfAccountPOS);
                    }
                } catch (Exception e1) {
                    // log.error(e1.getMessage());
                }
            });
            f = OldDateUntil.exit(f, vernier, e);
        }
    }
}